﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Input;
using WPFExcelReport.Helpers;
using System.Windows.Controls;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TEXARTLIST
    {
        #region ==IsPrint===========
        public static readonly DependencyProperty IsPrintProperty = DependencyProperty.Register("IsPrint",
            typeof(bool), typeof(TEXARTLIST),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsPrintChanged));
        public bool IsPrint
        {
            get
            {
                return (bool)GetValue(IsPrintProperty);
            }
            set
            {
                SetValue(IsPrintProperty, value);
                OnPrintChg();
            }
        }
        private static void OnIsPrintChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TEXARTLIST;
            if (src != null)
            {
                src.IsPrint = (bool)e.NewValue;
            }
        }

        public void OnPrintChg()
        {
            if (TMV == null)
                return;
            TMV.IsPrint = IsPrint;
            if (IsPrint)
                return;
            TBDetail.Merge(TBMain);
            TBDetail.Clear();
        }
        #endregion
        DataTable TBResult;
        public TEXARTLIST()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVAART();
            this.DataContext = MV;
            InitializeComponent();
        }

        public void PrintExCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                DataSet Set = new DataSet();
                DataTable TB = TBMain.Clone();
                TB.TableName = TMV.TableName;
                if (TBDetail.Rows.Count == 0)
                    return;
                TB.Merge(TBDetail);
                foreach (DataRow dwWB in TBDetail.Rows)
                {
                    dwData = TMV.VWS.TBMain.NewRow();
                    dwData["WAMANDANT"] = app.Mandant;
                    dwData["WAART"] = dwWB["VAADART"].ToString();
                    SResult = TMV.VWS.QWS13(dwData);
                    Set.Merge(SResult);
                }

                TB.TableName = "VAART";
                Set.Merge(TB);
                cDR.PrintType = "OpenExcel";
                cDR.DocType = VALBTYP;
                cDR.PRINT(Set);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void PrintCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                DataSet Set = new DataSet();
                DataTable TB = TBMain.Clone();
                TB.TableName = TMV.TableName;
                TB.Merge(TBDetail);
                foreach (DataRow dwWB in TBDetail.Rows)
                {
                    dwData = TMV.VWS.TBMain.NewRow();
                    dwData["WAMANDANT"] = app.Mandant;
                    dwData["WAART"] = dwWB["VAADART"].ToString();
                    SResult = TMV.VWS.QWS13(dwData);
                    Set.Merge(SResult);
                }
                TB.TableName = "VAART";
                Set.Merge(TB);
                cDR.DocType = VALBTYP;
                cDR.PrintType = "DirectPrint";
                cDR.PRINT(Set);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void BatchDeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {

                foreach (DataRow dwWB in TBDetail.Rows)
                {
                    filter = "VAAART='" + dwWB["VAAART"].ToString() + "'";
                    rows = TBMain.Select(filter);
                    if (rows.Length > 0) rows[0]["VAASPERRDAT"] = TMV.date;
                    dwWB["VAASPERRDAT"] = TMV.date;
                    TMV.UVAERRDAT(dwWB);
                }
                TBDetail.AcceptChanges();
                TBMain.AcceptChanges();

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void BarCode_KeyUp(object sender, KeyEventArgs e)
        {

            IsPrint = true;
            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (e.Key != System.Windows.Input.Key.Return)
                return;
            this.Focus();
            TextBox tb = sender as TextBox;
            ART = tb.Text;
            LOADAFART();
            if (TBDetail.Rows.Count == 0)
            {
                TBDetail.Merge(TBMain);
                TBDetail.Clear();
                if (vwDetail != null)
                    vwDetail.Source = TBDetail;
            }
            TBDetail.Merge(TBMain);
            tb.Focus();
        }
        bool IsOnline = true;
        public void LOADAFART()
        {
            try
            {
                if (IsOnline)
                {


                    dwData = TBMain.NewRow();
                    dwData["VAAMANDANT"] = app.Mandant;
                    dwData["VAAKOLL"] = "";
                    dwData["VAAARTST"] = "F";
                    dwData["VAAART"] = ((VVAART)MV).ART.Trim();
                    dwData["VAABEZ"] = ((VVAART)MV).DES.Trim();
                    SResult = AFSPageLoad(dwData);
                    MergeData(SResult, false);
                    IsBusy = false; 
                    SHIFT = "";
                }
            }
            catch (Exception ex)
            {
                IsOnline = false;
                Tracer.Error(ex.StackTrace, ex);

            }
            if (!IsOnline)
            {
                try
                {
                    string sql = "SELECT ARTICLE AS VAAART, COMPOSITION AS VAAFILLER,DESCRIPTION AS VAABEZ, m.* FROM ICC_EXART m WHERE RTRIM(ARTICLE) = '" + ((VVAART)MV).ART.Trim() + "'";
                    DataTable TBResult = TMV.SQLiteDatabase.GetDataTable(sql);
                    TBMain.Clear();
                    TBMain.Merge(TBResult);
                }
                catch(Exception ex)
                {
                    Tracer.Error(ex.StackTrace, ex);
                }
               
            }
        }
        public override void OnEXAUFChg(string value)
        {
            if (!IsPrint)
                return;
            if (value.Trim() == "")
                return;
            ART = value.PadRight(29).Substring(5, 24);
            LOADAFART();
            if (TBDetail.Rows.Count == 0)
            {
                TBDetail.Merge(TBMain);
                TBDetail.Clear();
                if (vwDetail != null)
                    vwDetail.Source = TBDetail;
            }
            TBDetail.Merge(TBMain);
            base.OnEXAUFChg(value);
        }

    }
}